package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MyReleaseBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.google.gson.Gson;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URL;

/**
 * Created by lihao on 2017/9/12.
 */

public class MyReleasePresenter extends basePresenter {

    private Contract.MyReleaseView myReleaseView;

    public MyReleasePresenter(Contract.MyReleaseView view) {
        super(view);
        this.myReleaseView = view;
    }

    public void initData(final boolean isReferesh, boolean dialogShow, final int page) {

        RequestParams rp = new RequestParams(URLS.HOST+URLS.MYRELEASE_LIST);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("page",page+"");

        x.http().post(rp, new myCallback(BaseActivity.context, MyReleaseBean.class,dialogShow,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MyReleaseBean bean = (MyReleaseBean) baseBean;
                if (isReferesh){
                    myReleaseView.OnRefresh(isReferesh,bean);
                }else {
                    myReleaseView.OnLoadMore(isReferesh,bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myReleaseView.OnFail(isReferesh);
            }

        });
    }

    public void deleteMyRelease(final int position, String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.DELETE_MY_RELEASE);
        rp.addBodyParameter("id",id);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context,MyBaseBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                myReleaseView.deleteSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }
}
